<!DOCTYPE html>
<html>
<head>
    <title>Op-Amp Circuit Diagram</title>
</head>
<body>
    <canvas id="circuitCanvas" width="600" height="420"></canvas>
    <script>
        const canvas = document.getElementById('circuitCanvas');
        const ctx = canvas.getContext('2d');

        // Style settings
        ctx.strokeStyle = 'black';
        ctx.lineWidth = 1.5;
        ctx.fillStyle = 'black';

        // --- Helper Functions ---

        function drawLine(x1, y1, x2, y2) {
            ctx.beginPath();
            ctx.moveTo(x1, y1);
            ctx.lineTo(x2, y2);
            ctx.stroke();
        }

        function drawResistor(x, y, w, h) {
            ctx.strokeRect(x, y - h / 2, w, h);
        }

        function drawOpAmp(x_in, y_mid, width, height) {
            const x_out = x_in + width;
            const y_top = y_mid - height / 2;
            const y_bot = y_mid + height / 2;

            ctx.beginPath();
            ctx.moveTo(x_in, y_top);
            ctx.lineTo(x_in, y_bot);
            ctx.lineTo(x_out, y_mid);
            ctx.closePath();
            ctx.stroke();

            // Draw '+' and '-' signs
            const sign_x = x_in + width * 0.15;
            const sign_y_plus = y_mid + height * 0.2;
            const sign_y_minus = y_mid - height * 0.2;
            
            ctx.font = '20px Arial';
            ctx.textAlign = 'center';
            ctx.textBaseline = 'middle';
            ctx.fillText('−', sign_x, sign_y_minus);
            ctx.fillText('+', sign_x, sign_y_plus);
        }

        function drawTerminal(x, y, radius = 4) {
            ctx.beginPath();
            ctx.arc(x, y, radius, 0, 2 * Math.PI);
            ctx.fillStyle = 'white';
            ctx.fill();
            ctx.stroke();
        }

        function drawNode(x, y, radius = 3) {
            ctx.beginPath();
            ctx.arc(x, y, radius, 0, 2 * Math.PI);
            ctx.fillStyle = 'black';
            ctx.fill();
        }

        function drawArrow(fromx, fromy, tox, toy) {
            const headlen = 10;
            const dx = tox - fromx;
            const dy = toy - fromy;
            const angle = Math.atan2(dy, dx);
            ctx.beginPath();
            ctx.moveTo(fromx, fromy);
            ctx.lineTo(tox, toy);
            ctx.stroke();
            ctx.beginPath();
            ctx.moveTo(tox, toy);
            ctx.lineTo(tox - headlen * Math.cos(angle - Math.PI / 6), toy - headlen * Math.sin(angle - Math.PI / 6));
            ctx.lineTo(tox - headlen * Math.cos(angle + Math.PI / 6), toy - headlen * Math.sin(angle + Math.PI / 6));
            ctx.closePath();
            ctx.fillStyle = 'black';
            ctx.fill();
        }

        function drawGround(x, y, size = 15) {
            drawLine(x, y, x, y + size);
            drawLine(x - size, y + size, x + size, y + size);
            drawLine(x - size * 0.6, y + size * 1.5, x + size * 0.6, y + size * 1.5);
            drawLine(x - size * 0.3, y + size * 2.0, x + size * 0.3, y + size * 2.0);
        }

        // --- Main Drawing ---

        // Define layout coordinates
        const y_gnd = 350;
        const y_in_line = 180;
        const y_opamp_mid = 200;
        const y_feedback_line = 100;
        const y_out_line = 200;

        const x_vin_term = 90;
        const x_r1_start = 120;
        const x_r1_end = 200;
        const x_node_in = 240;
        const x_opamp_in = 280;
        const x_opamp_out = 380;
        const x_node_out = 460;
        const x_vout_term = 510;
        
        // Ground line
        drawLine(40, y_gnd, 540, y_gnd);
        drawGround(420, y_gnd);
        
        // Input side
        drawTerminal(x_vin_term, y_in_line);
        drawLine(x_vin_term, y_in_line, x_r1_start, y_in_line);
        drawResistor(x_r1_start, y_in_line, x_r1_end - x_r1_start, 16);
        drawLine(x_r1_end, y_in_line, x_node_in, y_in_line);
        drawNode(x_node_in, y_in_line);

        // Op-Amp
        drawOpAmp(x_opamp_in, y_opamp_mid, 100, 80);
        
        // Connections to op-amp
        const y_opamp_minus = y_opamp_mid - 20;
        const y_opamp_plus = y_opamp_mid + 20;
        drawLine(x_node_in, y_in_line, x_opamp_in, y_opamp_minus); // Inverting input
        const x_plus_gnd_connect = 200; // X-coord for vertical wire to ground
        drawLine(x_opamp_in, y_opamp_plus, x_plus_gnd_connect, y_opamp_plus);
        drawLine(x_plus_gnd_connect, y_opamp_plus, x_plus_gnd_connect, y_gnd);
        drawNode(x_plus_gnd_connect, y_gnd);

        // Feedback loop
        drawLine(x_node_in, y_in_line, x_node_in, y_feedback_line);
        drawResistor(x_node_in, y_feedback_line, x_node_out - x_node_in, 16);
        drawLine(x_node_out, y_feedback_line, x_node_out, y_out_line);
        drawNode(x_node_out, y_out_line);

        // Output side
        drawLine(x_opamp_out, y_out_line, x_node_out, y_out_line);
        drawLine(x_node_out, y_out_line, x_vout_term, y_out_line);
        drawTerminal(x_vout_term, y_out_line);
        
        // --- Labels and Arrows ---
        
        // V_IN
        drawArrow(60, y_gnd, 60, y_in_line);
        ctx.font = 'italic 20px "Times New Roman"';
        ctx.textAlign = 'right';
        ctx.textBaseline = 'middle';
        ctx.fillText('V', 55, (y_gnd + y_in_line) / 2);
        ctx.font = 'italic 14px "Times New Roman"';
        ctx.fillText('IN', 62, (y_gnd + y_in_line) / 2 + 7);

        // V_OUT
        drawArrow(540, y_gnd, 540, y_out_line);
        ctx.font = 'italic 20px "Times New Roman"';
        ctx.textAlign = 'left';
        ctx.fillText('V', 545, (y_gnd + y_out_line) / 2);
        ctx.font = 'italic 14px "Times New Roman"';
        ctx.fillText('OUT', 556, (y_gnd + y_out_line) / 2 + 7);
        
        // Component values and other labels
        ctx.font = '20px "Times New Roman"';
        ctx.textAlign = 'center';
        ctx.textBaseline = 'bottom';
        ctx.fillText('800 Ω', (x_r1_start + x_r1_end) / 2, y_in_line - 12);
        ctx.fillText('9600 Ω', (x_node_in + x_node_out) / 2, y_feedback_line - 12);
        
        const opamp_power_x = x_opamp_in + 50;
        ctx.fillText('+6 V', opamp_power_x, 150);
        ctx.textBaseline = 'top';
        ctx.fillText('−6 V', opamp_power_x, 250);

        // Figure caption
        ctx.font = 'bold 20px "Times New Roman"';
        ctx.textAlign = 'center';
        ctx.textBaseline = 'bottom';
        ctx.fillText('Fig. 8.1', canvas.width / 2, 410);

    </script>
</body>
</html>